package me.passtime.projt.query;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import me.passtime.projt.Home;
import me.passtime.projt.R;
import me.passtime.projt.util.AlertDialogUtils;
import me.passtime.projt.util.PageJumpUtils;
import me.passtime.projt.util.SystemUtils;
import me.passtime.projt.util.UserHolder;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-7
 * Time: 下午2:05
 */
public class QueryAvailableBalancePage extends Activity implements View.OnClickListener {
    private Button mUserInfo;
    private TextView mPhoneNumber;
    private TextView mBalanceAmount;
    private Handler mHandler;

    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 7;
    private QueryController mController;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.query_available_balance);
        mController=new QueryController(this);
        mHandler = new Handler();
        initData();
        queryAvailableBalance();
        this.findViewById(R.id.available_balance_back_btn).setOnClickListener(this);
        this.findViewById(R.id.available_balance_logout_btn).setOnClickListener(this);
        this.findViewById(R.id.available_balance_my_account_btn).setOnClickListener(this);
        this.findViewById(R.id.available_balance_fee_records_btn).setOnClickListener(this);
        this.findViewById(R.id.available_balance_exit).setOnClickListener(this);
    }

    private void initData() {
        mUserInfo = (Button) this.findViewById(R.id.available_balance_person_btn);
        mUserInfo.setText(UserHolder.phoneNumber + "\r\n" + UserHolder.userName);
        mPhoneNumber = (TextView) this.findViewById(R.id.available_balance_phone_number);
        mPhoneNumber.setText(UserHolder.phoneNumber);
        mBalanceAmount = (TextView) this.findViewById(R.id.available_balance_amount);
    }

    private void queryAvailableBalance() {
        showDialog(DIALOG_PROGRESS);
        mController.queryAB(UserHolder.phoneNumber);
    }
    
    public void updateAvailableBalanceUI(String value){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mBalanceAmount.setText("186");
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.available_balance_back_btn:
                PageJumpUtils.jump(this, Home.class);
                break;
            case R.id.available_balance_logout_btn:
                PageJumpUtils.logoutAndJumpToMainPage(this);
                break;
            case R.id.available_balance_my_account_btn:
                PageJumpUtils.jumpToManageAccountPage(this);
                break;
            case R.id.available_balance_fee_records_btn:
                PageJumpUtils.jumpToQueryPaymentPage(this);
                break;
            case R.id.available_balance_exit:
                AlertDialogUtils.showYesNoDiaLog(this, "是否退出系统?", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        SystemUtils.killSelf();
                        finish();
                    }
                });
                break;
        }
    }

    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
